/**
 * Created by chenyong on 2015/11/27.
 */

//importScripts('/libs/socket.io-1.3.7.js');

onmessage = function(event) {
    if(event.data.msgtype == "getusertags")
    {
        var panoid = event.data.panoid;
        var skip = event.data.skip;
        var limit = event.data.limit;
        var gendar = event.data.gendar;
        getusertags(panoid,skip,limit,gendar);
    }
    if(event.data.msgtype == "randomusertags")
    {
        var panoid = event.data.panoid;
        var count = event.data.count || 10;
        getRandomUserTags(panoid,count);
        //socket.emit('randomusertags',panoid,count);
    }
    //getUserTagsArray(event.data.panoid,event.data.limit,event.data.skip);
};





//转换TagInfo格式
function convertTaginfo(data)
{
    var _taginfo = {};
    _taginfo.id = "usertag" + data._id;
    _taginfo.ath = data.ath;
    _taginfo.atv = data.atv;
    _taginfo.fov = data.fov;
    if(typeof(data.userinfo) == "object" )
    {
        _taginfo.avatarurl =  data.userinfo.headimgurl;
    }
    else
    {
        _taginfo.avatarurl = "/images/noavatar@96x96.jpg";
    }

    _taginfo.title = data.title;
    _taginfo.note = data.note;

  if(data.attached != undefined && typeof(data.attached) == "string")
  {
    _taginfo.attached = JSON.parse(data.attached);
  }
  else if (data.attached != undefined && typeof(data.attached == "object") )
  {
    _taginfo.attached = data.attached;
  }


    return _taginfo;
}

function getusertags(panoid,skip,limit,gendar)
{
    that = self;
    var _url = "/service/panos/" + panoid + "/usertags?skip=" + skip + "&limit=" + limit + "&gendar=" + gendar;
    fetch(_url,function(err,xhr){
        if(!err && xhr.status && xhr.status == "200")
        {
            var retdata = JSON.parse(xhr.responseText);
            var data = retdata.data;
            var tags = [];
            for(var i=0;i<data.length;i++)
            {
                var _taginfo = convertTaginfo(data[i]);
                tags.push(_taginfo);
            }
            var result = {msgtype:"getusertags",list:tags,count:data.length,total:retdata.total,skip:retdata.skip};
            that.postMessage(result);
        }
        else
        {
            var result = {msgtype:"getusertags-error",err:"读取标注点信息出错"};
            that.postMessage(result);
        }
    })
}

function getRandomUserTags(panoid,count)
{
    that = self;
    var _url = "/service/panos/" + panoid + "/usertags/randomusertags?count=" + count;
    fetch(_url,function(err,xhr){
        if(!err && xhr.status && xhr.status == "200")
        {
            var data = JSON.parse(xhr.responseText);
            var tags = [];
            for(var i=0;i<data.length;i++)
            {
                var _taginfo = convertTaginfo(data[i]);
                tags.push(_taginfo);
            }
            var result = {msgtype:"randomusertags",list:tags,len:data.length};
            that.postMessage(result);
        }
        else
        {
            var result = {msgtype:"randomusertags-error",err:"读取随机标注点出错"};
            that.postMessage(result);
        }
    })
}


function fetch(url, callback) {
    var xhr;
    if(typeof XMLHttpRequest !== 'undefined') xhr = new XMLHttpRequest();
    else {
        var versions = ["MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp"];

        for(var i = 0, len = versions.length; i < len; i++) {
            try {
                xhr = new ActiveXObject(versions[i]);
                break;
            }
            catch(e){}
        }
    }
    xhr.onreadystatechange = ensureReadiness;
    function ensureReadiness() {

        if(xhr.readyState < 4) {
            return;
        }
        if(xhr.status !== 200) {
            console.log("fetch error");
            console.log(xhr);
            var err = new Error("XMLHttpRequest错误：" + xhr.status);
            callback(err,null)
            return;
        }
        if(xhr.readyState === 4) {
            callback(null,xhr);
        }
    }
    xhr.open('GET', url, true);
    xhr.send('');
}

